import PySide6
from PySide6.QtWidgets import QMainWindow
from PySide6.QtGui import QAction, QIcon
from easy_tools.ui.welcome_widget import WelcomeWidget
from easy_tools.ui.file.string_replace_widget import StringReplaceWidget
from easy_tools.ui.normal.timestamp_conversion_widget import TimestampConversionWidget
from easy_tools.ui.file.file_conversion.text_file_conversion_widget import TextFileConversionWidget
from easy_tools.ui.file.file_conversion.pic_base64_conversion_widget import PictureToBase64Widget, Base64ToPictureWidget
from easy_tools.ui.system.system_storage_scan_widget import SystemStorageScanWidget
from easy_tools.ui.development.json_to_code_widget import JsonToCodeWidget


class MainFrame(QMainWindow):

    def __init__(self,
                 title='咸鱼便捷工具',
                 icon='./statics/images/favicon.ico',
                 qr_code='./statics/images/wechat_person.jpg',
                 widget=None):
        super().__init__()
        self.title = title
        self.icon = icon
        self.qr_code = qr_code
        if widget:
            self.setCentralWidget(widget)
        self._init()

    def _init(self):
        self.setWindowTitle(self.title)
        self.setWindowIcon(QIcon(self.icon))
        self._init_menu()
        self._open_welcome_conversion_handle()

    def _init_menu(self):
        self.menu = self.menuBar()
        # 文件相关
        replace_str_action = QAction('&文本内容替换', self)
        # replace_str_action.setShortcut('Ctrl+O')
        # replace_str_action.setStatusTip('打开目录/文件')
        replace_str_action.triggered.connect(self._open_dir_or_file_handle)

        text_file_conversion_action = QAction('&文本文件类型转换', self)
        text_file_conversion_action.triggered.connect(self._open_file_conversion_handle)
        pic_to_base64_action = QAction('&图片转base64', self)
        pic_to_base64_action.triggered.connect(self._open_pic_to_base64_handle)
        base64_to_pic_action = QAction('&base64转图片', self)
        base64_to_pic_action.triggered.connect(self._open_base64_to_pic_handle)

        dir_or_file_menu = self.menu.addMenu('&目录/文件管理')
        dir_or_file_menu.addAction(replace_str_action)
        file_conversion_menu = dir_or_file_menu.addMenu('&文件类型转换')
        file_conversion_menu.addAction(text_file_conversion_action)
        file_conversion_menu.addAction(pic_to_base64_action)
        file_conversion_menu.addAction(base64_to_pic_action)

        # 通用相关
        timestamp_conversion_action = QAction('&时间戳转换工具', self)
        timestamp_conversion_action.triggered.connect(self._open_timestamp_conversion_handle)
        normal_menu = self.menu.addMenu('&通用工具')
        normal_menu.addAction(timestamp_conversion_action)

        # 开发工具
        json_to_code_action = QAction('&Json转代码工具', self)
        json_to_code_action.triggered.connect(self._open_json_to_code_handle)
        development_menu = self.menu.addMenu('&开发工具')
        development_menu.addAction(json_to_code_action)

        # 系统相关
        system_storage_scan_action = QAction('&磁盘存储扫描工具', self)
        system_storage_scan_action.triggered.connect(self._open_system_storage_scan_handle)
        system_menu = self.menu.addMenu('&系統工具')
        system_menu.addAction(system_storage_scan_action)

    def _center(self):
        win_rect = self.frameGeometry()  # 窗口矩形（QRect类）
        scr_center = self.screen().availableGeometry().center()  # 屏幕中心
        win_rect.moveCenter(scr_center)  # 窗口矩形中心移动到屏幕中心
        self.move(win_rect.topLeft())  # 移动窗口和窗口矩形重合

    def _open_dir_or_file_handle(self):
        self._clear_central_widget()
        self.resize(700, 160)
        self.setWindowTitle(self.title + ' - ' + '文本内容替换工具')
        self.setCentralWidget(StringReplaceWidget())
        self._center()

    def _open_timestamp_conversion_handle(self):
        self._clear_central_widget()
        self.resize(350, 160)
        self.setWindowTitle(self.title + ' - ' + '时间戳转换工具')
        self.setCentralWidget(TimestampConversionWidget())
        self._center()

    def _open_file_conversion_handle(self):
        self._clear_central_widget()
        self.resize(600, 170)
        self.setWindowTitle(self.title + ' - ' + '文件转换工具')
        self.setCentralWidget(TextFileConversionWidget())
        self._center()

    def _open_welcome_conversion_handle(self):
        self._clear_central_widget()
        self.resize(800, 600)
        self.setWindowTitle(self.title)
        self.setCentralWidget(WelcomeWidget(self.qr_code))
        self._center()

    def _open_pic_to_base64_handle(self):
        self._clear_central_widget()
        self.resize(700, 300)
        self.setWindowTitle(self.title + ' - ' + '图片转Base64')
        self.setCentralWidget(PictureToBase64Widget())
        self._center()

    def _open_base64_to_pic_handle(self):
        self._clear_central_widget()
        self.resize(700, 350)
        self.setWindowTitle(self.title + ' - ' + 'Base64转图片')
        self.setCentralWidget(Base64ToPictureWidget())
        self._center()

    def _open_json_to_code_handle(self):
        self._clear_central_widget()
        self.resize(800, 600)
        self.setWindowTitle(self.title + ' - ' + 'Json生成代码工具')
        self.setCentralWidget(JsonToCodeWidget())
        self._center()

    def _open_system_storage_scan_handle(self):
        self._clear_central_widget()
        self.setMinimumSize(600, 120)
        self.setMaximumSize(600, 600)

        self.setWindowTitle(self.title + ' - ' + '磁盘扫描工具')
        self.setCentralWidget(SystemStorageScanWidget())
        self._center()

    def _clear_central_widget(self):
        w = self.centralWidget()
        if w:
            w.close()
            self.resize(0, 0)

    def closeEvent(self, event: PySide6.QtGui.QCloseEvent) -> None:
        self._clear_central_widget()
        super().closeEvent(event)

    def close(self) -> bool:
        self._clear_central_widget()
        return super().close()

